package pl.gda.pg.eti.mkdg.projekt;

import javax.swing.*;
import javax.swing.border.*;
import java.awt.image.*;
import java.awt.event.*;

public class FragmentSelector extends JFrame implements MouseMotionListener, MouseListener
{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private int               mouseX;
    private int               mouseY;
    private boolean           selectingLT;
    private boolean           selectingRB;
    private int               oldX;
    private int               oldY;
    private JPanel            contentPane;
    private Main              main;
    private BufferedImage     imgIn;
    public JScrollPane        scrollPane;
    public JLabel             label;
    public JButton            button;
    public JPanel             panel;
    public JLabel             label_1;
    public JPanel             panel_1;
    public JLabel             label_2;
    public JLabel             label_3;
    public JSpinner           spinnerLTX;
    public JSpinner           spinnerLTY;
    public JLabel             label_4;
    public JLabel             label_5;
    public JSpinner           spinnerRBX;
    public JLabel             label_6;
    public JSpinner           spinnerRBY;
    public JLabel             label_7;
    public JButton            button_1;
    public JButton            button_2;
    public JButton            button_3;
    public JLabel             label_8;
    public JLabel             label_9;

    /**
     * Create the frame.
     */
    public FragmentSelector(Main m)
    {
        main = m;
        setType(Type.UTILITY);
        setResizable(false);
        setBounds(100, 100, 800, 600);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        this.addMouseListener(this);

        scrollPane = new JScrollPane();
        scrollPane.setBounds(10, 10, 555, 555);
        contentPane.add(scrollPane);

        label = new JLabel("");
        label.setHorizontalAlignment(SwingConstants.LEFT);
        label.setVerticalAlignment(SwingConstants.TOP);
        label.addMouseMotionListener(this);
        label.addMouseListener(this);
        scrollPane.setViewportView(label);

        button = new JButton("Wybierz caly obraz");
        button.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent arg0)
            {
                buttonActionPerformed(arg0);
            }
        });
        button.setBounds(575, 10, 209, 23);
        contentPane.add(button);

        panel = new JPanel();
        panel.setBounds(575, 44, 209, 521);
        contentPane.add(panel);
        panel.setLayout(null);

        label_1 = new JLabel("Wyb\u00F3r fragmentu obrazu:");
        label_1.setBounds(10, 11, 189, 16);
        panel.add(label_1);

        panel_1 = new JPanel();
        panel_1.setBounds(10, 38, 189, 472);
        panel.add(panel_1);
        panel_1.setLayout(null);

        label_2 = new JLabel("Lewy g\u00F3rny r\u00F3g:");
        label_2.setBounds(10, 11, 169, 20);
        panel_1.add(label_2);

        label_3 = new JLabel("X:");
        label_3.setBounds(10, 45, 10, 14);
        panel_1.add(label_3);

        spinnerLTX = new JSpinner();
        spinnerLTX.setModel(new SpinnerNumberModel(0, 0, 0, 1));
        spinnerLTX.setBounds(30, 42, 49, 20);
        panel_1.add(spinnerLTX);

        spinnerLTY = new JSpinner();
        spinnerLTY.setBounds(109, 42, 49, 20);
        panel_1.add(spinnerLTY);

        label_4 = new JLabel("Y:");
        label_4.setBounds(89, 45, 10, 14);
        panel_1.add(label_4);

        label_5 = new JLabel("X:");
        label_5.setBounds(10, 104, 10, 14);
        panel_1.add(label_5);

        spinnerRBX = new JSpinner();
        spinnerRBX.setBounds(30, 101, 49, 20);
        panel_1.add(spinnerRBX);

        label_6 = new JLabel("Y:");
        label_6.setBounds(89, 104, 10, 14);
        panel_1.add(label_6);

        spinnerRBY = new JSpinner();
        spinnerRBY.setBounds(109, 101, 49, 20);
        panel_1.add(spinnerRBY);

        label_7 = new JLabel("Prawy dolny r\u00F3g:");
        label_7.setBounds(10, 70, 169, 20);
        panel_1.add(label_7);

        button_1 = new JButton("Wczytaj fragment");
        button_1.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent arg0)
            {
                button_1ActionPerformed(arg0);
            }
        });
        button_1.setBounds(10, 131, 169, 23);
        panel_1.add(button_1);

        button_2 = new JButton("Wybierz lewy g\u00F3rny r\u00F3g");
        button_2.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent arg0)
            {
                button_2ActionPerformed(arg0);
            }
        });
        button_2.setBounds(10, 167, 169, 23);
        panel_1.add(button_2);

        button_3 = new JButton("Wybierz prawy dolny r\u00F3g");
        button_3.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                button_3ActionPerformed(e);
            }
        });
        button_3.setBounds(10, 203, 169, 23);
        panel_1.add(button_3);

        label_8 = new JLabel("LPM zatwierdza");
        label_8.setBounds(10, 239, 169, 14);
        panel_1.add(label_8);

        label_9 = new JLabel("PPM anuluje");
        label_9.setBounds(10, 266, 169, 14);
        panel_1.add(label_9);
    }

    public void OpenWithImage(BufferedImage img)
    {
        imgIn = img;
        label.setIcon(new ImageIcon(imgIn));

        SpinnerNumberModel m = (SpinnerNumberModel) spinnerLTX.getModel();
        m.setMaximum(imgIn.getWidth() - 1);
        spinnerLTX.setModel(m);
        spinnerLTX.setValue(0);

        m = (SpinnerNumberModel) spinnerLTY.getModel();
        m.setMaximum(imgIn.getHeight() - 1);
        spinnerLTY.setModel(m);
        spinnerLTY.setValue(0);

        m = (SpinnerNumberModel) spinnerRBX.getModel();
        m.setMaximum(imgIn.getWidth() - 1);
        spinnerRBX.setModel(m);
        spinnerRBX.setValue(imgIn.getWidth() - 1);

        m = (SpinnerNumberModel) spinnerRBY.getModel();
        m.setMaximum(imgIn.getHeight() - 1);
        spinnerRBY.setModel(m);
        spinnerRBY.setValue(imgIn.getHeight() - 1);

        setVisible(true);
    }

    protected void buttonActionPerformed(ActionEvent arg0)
    {
        main.LoadImage(imgIn);
        setVisible(false);
    }

    protected void button_1ActionPerformed(ActionEvent arg0)
    {
        int x1 = (Integer) spinnerLTX.getValue();
        int y1 = (Integer) spinnerLTY.getValue();
        int x2 = (Integer) spinnerRBX.getValue();
        int y2 = (Integer) spinnerRBY.getValue();

        if (x1 == x2)
            x2 = x1 + 1;
        else if (x1 > x2)
        {
            int tmp = x1;
            x1 = x2;
            x2 = tmp;
        }

        if (y1 == y2)
            y2 = y1 + 1;
        else if (y1 > y2)
        {
            int tmp = y1;
            y1 = y2;
            y2 = tmp;
        }

        BufferedImage imgOut = imgIn.getSubimage(x1, y1, x2 - x1, y2 - y1);
        main.LoadImage(imgOut);
        setVisible(false);
    }

    @Override
    public void mouseDragged(MouseEvent e)
    {
    }

    @Override
    public void mouseMoved(MouseEvent e)
    {
        mouseX = e.getX();
        mouseY = e.getY();

        if (mouseX > imgIn.getWidth() - 1)
            mouseX = imgIn.getWidth() - 1;
        if (mouseY > imgIn.getHeight() - 1)
            mouseY = imgIn.getHeight() - 1;

        if (selectingLT)
        {
            spinnerLTX.setValue(mouseX);
            spinnerLTY.setValue(mouseY);
        }
        if (selectingRB)
        {
            spinnerRBX.setValue(mouseX);
            spinnerRBY.setValue(mouseY);
        }
    }

    @Override
    public void mouseClicked(MouseEvent e)
    {
        if (e.getButton() == MouseEvent.BUTTON1)
        {
            if (selectingLT)
            {
                spinnerLTX.setValue(mouseX);
                spinnerLTY.setValue(mouseY);
            }
            if (selectingRB)
            {
                spinnerRBX.setValue(mouseX);
                spinnerRBY.setValue(mouseY);
            }
        }
        else
        {
            if (selectingLT)
            {
                spinnerLTX.setValue(oldX);
                spinnerLTY.setValue(oldY);
            }
            if (selectingRB)
            {
                spinnerRBX.setValue(oldX);
                spinnerRBY.setValue(oldY);
            }
        }

        selectingLT = false;
        selectingRB = false;
    }

    @Override
    public void mouseEntered(MouseEvent e)
    {
    }

    @Override
    public void mouseExited(MouseEvent e)
    {
    }

    @Override
    public void mousePressed(MouseEvent e)
    {
    }

    @Override
    public void mouseReleased(MouseEvent e)
    {
    }

    protected void button_2ActionPerformed(ActionEvent arg0)
    {
        if (!selectingLT && !selectingRB)
        {
            selectingLT = true;
            oldX = (Integer) spinnerLTX.getValue();
            oldY = (Integer) spinnerLTY.getValue();
        }
    }

    protected void button_3ActionPerformed(ActionEvent e)
    {
        if (!selectingLT && !selectingRB)
        {
            selectingRB = true;
            oldX = (Integer) spinnerRBX.getValue();
            oldY = (Integer) spinnerRBY.getValue();
        }
    }
}
